package com.naiterui.ehp.bs.patient.service.impl;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import com.naiterui.common.redis.RedisUtil;
import com.naiterui.ehp.bp.constants.CommonConstant;
import com.naiterui.ehp.bp.constants.OrderSnType;
import com.naiterui.ehp.bp.domain.Patient;
import com.naiterui.ehp.bp.support.exception.BusinessException;
import com.naiterui.ehp.bp.support.security.HeaderConstant;
import com.naiterui.ehp.bp.utils.payment.OrderUtils;
import com.naiterui.ehp.bp.utils.payment.bo.RefundRequestBO;
import com.naiterui.ehp.bp.utils.payment.bo.UnifiedOrderRequestBO;
import com.naiterui.ehp.bp.utils.payment.bo.UnifiedOrderResultBO;
import com.naiterui.ehp.bs.patient.dao.IPatientDao;
import com.naiterui.ehp.bs.patient.domain.WeixinInfo;
import com.naiterui.ehp.bs.patient.repository.IWeixinInfoRepository;
import com.naiterui.ehp.bs.patient.service.IApSelfInspectService;
import com.naiterui.ehp.bs.patient.utils.ConfigKeyUtil;

import lombok.RequiredArgsConstructor;

/**
 * @author luao
 * @date 2022/4/25 10:14 AM
 **/
@Service
@RequiredArgsConstructor
public class ApSelfInspectServiceImpl implements IApSelfInspectService {

    private final IPatientDao patientDao;
    private final IWeixinInfoRepository weixinInfoRepository;

    public static final String SELF_INSPECT_ORDER_KEY = CommonConstant.REDIS_PRE_PT + "self_inspect_order_";
    /**
     * 患者域名
     */
    private static final String PATIENT_URL = ConfigKeyUtil.getString("patient.url");

    @Override
    public UnifiedOrderResultBO pay(Long patientId, String ip) throws BusinessException {
        Patient patient = patientDao.get(patientId);
        WeixinInfo weixinInfo = weixinInfoRepository.findByMiniOpenId(patient.getMiniOpenId());
        String orderSn = OrderUtils.getOrderSn(OrderSnType.SELF_INSPECT_TEST);
        RedisUtil.valueOps().setString(SELF_INSPECT_ORDER_KEY + patientId, orderSn);
        // 调用微信支付
        return OrderUtils.pay(UnifiedOrderRequestBO.builder()
            // 订单号
            .orderSn(orderSn)
            // 商品名称
            .body(orderSn)
            // 用户OPEN id
            .openId(weixinInfo.getMiniOpenId())
            // 用户ip
            .ip(ip)
            // 订单金额
            .totalFee(1)
            // 订单来源
            .origin(HeaderConstant.HEADER_ORIGIN_PATIENT_APP)
            // 业务通知地址
            .notifyBusinessUrl(PATIENT_URL + "/inner/notify/pay")
            // 订单类型
            .orderType(OrderSnType.SELF_INSPECT_TEST).build());
    }

    @Override
    public void refund(Long patientId) throws BusinessException {
        String orderSn = RedisUtil.valueOps().getString(SELF_INSPECT_ORDER_KEY + patientId);
        if (StringUtils.isEmpty(orderSn)) {
            return;
        }
        Integer price = 1;
        OrderUtils.refund(RefundRequestBO.builder()
            // 订单号
            .orderSn(orderSn).refundOrderSn(orderSn + "RE")
            // 订单金额
            .totalFee(price)
            // 退款金额
            .refundFee(price)
            // 业务回调地址
            .notifyBusinessUrl(PATIENT_URL + "/inner/notify/refund")
            .build());
    }
}
